package com.example.jh352160.surfingtv.Index.childpage;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.ListView;

import com.example.jh352160.surfingtv.R;
import com.example.jh352160.surfingtv.category.customView.Subscribe_item;
import com.example.jh352160.surfingtv.category.customView.adapter.SubscribeListAdapter;
import com.example.jh352160.surfingtv.network.item.ThematicListItem;
import com.example.jh352160.surfingtv.network.retrofit.CommonRetrofit;
import com.example.jh352160.surfingtv.network.service.ThematicListService;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * Created by jh352160 on 2016/7/28
 */

public class ThematicList extends AppCompatActivity {

    private static final int NETWORK_OK = 0;
    ListView thematicList;
    List<Subscribe_item> thematic_items;
    Toolbar toolbar;
    List<String> titleList, imageUrlList, aboutList, detailUrlList;
    List<Integer> commpdityContentIdList, favoriteCountList;
    List<Boolean> isFavoriteList;
    int contentId;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case NETWORK_OK:
                    init();
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.index_thematic_list);
        thematicList = (ListView) findViewById(R.id.categoryList);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("");
        Intent intent = getIntent();
        contentId = intent.getIntExtra("contentId", 0);

        //api
        Retrofit retrofit = CommonRetrofit.getRetrofit();
        ThematicListService thematicListService = retrofit.create(ThematicListService.class);
        Call<ThematicListItem> call = thematicListService.getItem("program-" + contentId);
        call.enqueue(new Callback<ThematicListItem>() {
            @Override
            public void onResponse(Call<ThematicListItem> call, Response<ThematicListItem> response) {
                List<ThematicListItem.DataBean.ItemsBean> items = response.body().getData().getItems();
                titleList = new ArrayList<>();
                imageUrlList = new ArrayList<>();
                aboutList = new ArrayList<>();
                detailUrlList = new ArrayList<>();
                commpdityContentIdList = new ArrayList<>();
                favoriteCountList = new ArrayList<>();
                isFavoriteList = new ArrayList<>();
                for (ThematicListItem.DataBean.ItemsBean i : items) {
                    titleList.add(i.getTitle());
                    imageUrlList.add(i.getImageUrl());
                    aboutList.add(i.getSummary());
                    detailUrlList.add(i.getDetailUrl());
                    commpdityContentIdList.add(i.getContentId());
                    favoriteCountList.add(i.getFavoriteCount());
                    isFavoriteList.add(i.getFavorite() == 1);
                }
                //更新界面&构建List
                Message message = new Message();
                message.what = NETWORK_OK;
                handler.sendMessage(message);
            }

            @Override
            public void onFailure(Call<ThematicListItem> call, Throwable t) {

            }
        });

//        String[] titles={"专题1","专题2","专题3","专题4"};
//        String[] abouts={"简介1","简介2","简介3","简介4"};
//        thematic_items =new ArrayList<>();
//        for(int i=0;i< titles.length;i++){
//            Subscribe_item subscribe_item=new Subscribe_item(this);
//            subscribe_item.setText(titles[i],abouts[i]);
//            thematic_items.add(subscribe_item);
//        }
//        SubscribeListAdapter adapter=new SubscribeListAdapter(this,R.layout.category_subscribe_item, thematic_items);
//        thematicList.setAdapter(adapter);
    }

    private void init() {
        thematic_items = new ArrayList<>();
        for (int i = 0; i < titleList.size(); i++) {
            Subscribe_item subscribe_item = new Subscribe_item(this);
            subscribe_item.setData(titleList.get(i), aboutList.get(i), imageUrlList.get(i), detailUrlList.get(i), favoriteCountList.get(i), isFavoriteList.get(i), commpdityContentIdList.get(i));
            thematic_items.add(subscribe_item);
        }
        SubscribeListAdapter adapter = new SubscribeListAdapter(this, R.layout.category_subscribe_item, thematic_items);
        thematicList.setAdapter(adapter);
    }
}
